﻿//using FirstFloor.ModernUI.Windows.Controls;
using KinectLibrary;
using KinectLibrary.Contour;
using KinectLibrary.Curves;
using KinectLibrary.Debug;
using KinectLibrary.DTWGestureRecognition;
using KinectLibrary.Enhancements;
using KinectLibrary.Fingers;
using KinectLibrary.TTS;
using Microsoft.Kinect;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SLA
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class PlayWindow : Window
    {
        private List<Gesture> gestures;

        public PlayWindow(KinectSensor sensor, List<Gesture> gestures, string gesto)
        {
            InitializeComponent();
            kinectRegion.KinectSensor = sensor;
            this.gestures = gestures;
            sdrGesto.Value = 3;
            sdrGestos.Value = 2;
            txtGestures.Text = gesto;
            btnPlay_Click(null, null);
        }

        private async Task DrawGesture(Gesture gesture)
        {
            var converter = new System.Windows.Media.BrushConverter();
            var brush = (Brush)converter.ConvertFromString("#FF007EFF");

            canvas.Children.Clear();
            canvas.Children.Add(new TextBlock()
            {
                Margin = new Thickness(20, 10, 10, 10),
                FontSize = 60,
                Text = gesture.Speech,
                Foreground = brush
            });
            var vecesGesto = Convert.ToInt32(Math.Ceiling(sdrGesto.Value));
            for (int i = 0; i < vecesGesto; i++)
            {
                foreach (var frame in gesture.Frames)
                {
                    DrawingHelper.DrawFrame(frame, canvas, DrawingType.SMALL);
                    await Task.Delay(20);
                }
                await Task.Delay(250);
            }
            await Task.Delay(500);
        }

        private void PlayWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private async void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            if (this.gestures != null)
            {
                var names = txtGestures.Text.Split(' ');
                
                var vecesGestos = Convert.ToInt32(Math.Ceiling(sdrGestos.Value));
                for (int i = 0; i < vecesGestos; i++)
                {
                    foreach (var name in names)
                    {
                        var gesture = this.gestures.FirstOrDefault(g => g.Name == name);
                        if (gesture != null)
                        {
                            await DrawGesture(gesture);
                        }
                    }
                }
            }
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
